package org.sparkle.observer;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.sparkle.ioc.Bean;
import org.sparkle.register.AbstractRegister;
import org.sparkle.register.Register;
import org.sparkle.register.RegisterObj;
import org.sparkle.scanner.AnnotationScanner;

@Register(name="ObRegister")
public class ObRegister extends AbstractRegister{
	
	private Map<String,List<Method>> methods; 
	
//	@SuppressWarnings("rawtypes")
//	@Override
//	public void populateClasses(Reflections reflections, Map<String, RegisterObj> map) {
//		
//		Set<Class<?>> annotatedClasses= reflections.getTypesAnnotatedWith(Bean.class);
//		Ob ann;
//		for(Class c: annotatedClasses){				
//			for(Method m: c.getMethods()){
//				ann=m.getAnnotation(Ob.class);
//				if(ann!=null)
//					addMethod(ann.type(),m);	
//			}
//		}
//		
//	}

	public void addMethod(String type,Method m){
		List<Method> list=getMethods().get(type);
		if(list==null){
			list=new ArrayList<Method>();
			getMethods().put(type, list);
		}
		
		list.add(m);
	}


	public Map<String, List<Method>> getMethods() {
		if(methods==null)
			methods=new HashMap<String,List<Method>>();
		return methods;
	}

	@Override
	public void populateInstances(AnnotationScanner scanner,
			Map<String, RegisterObj> map) {
		List<Class> annotatedClasses= scanner.getListByAnnotation(Bean.class);
		Ob ann;
		for(Class c: annotatedClasses){				
			for(Method m: c.getMethods()){
				ann=m.getAnnotation(Ob.class);
				if(ann!=null)
					addMethod(ann.value(),m);	
			}
		}
		
	}
	
}
